from django.shortcuts import render

# Create your views here.
from jie.models import JieInfo
from user.models import UserInfo


def add(request):
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')

    return render(request, 'jie/add.html', userinfo)


def detail(request):
    return render(request, 'jie/detail.html')


def index(request):
    return render(request, 'jie/index.html')


def doadd(request):
    id = request.session['userinfo']['user_id']
    u = UserInfo.objects.get(user_id=id)
    section = request.POST.get('class')
    titile = request.POST.get('title')
    content = request.POST.get('content')
    # comment = request.POST.get('sign')
    # date = request.POST.get('sign')
    kiss = request.POST.get('experience')
    j = JieInfo()
    j.jie_user = u
    j.jie_section = section
    j.jie_titile = titile
    j.jie_content = content
    j.jie_kiss = kiss
    j.save()
    jieinfo = JieInfo.objects.all()
    list1 = []
    for i in jieinfo:
        content = {
            'user_name': i.jie_user.user_name,
            'user_photo': i.jie_user.user_photo.url,
            'jie_section': i.jie_section,
            'jie_title': i.jie_titile,
            'jie_date': str(i.jie_date),
            'jie_kiss': i.jie_kiss,
            'jie_comment': i.jie_comment,
        }
        list1.append(content)
        list1.reverse()
    request.session['jieinfo'] = list1

    return render(request, 'index.html', content)


def dodetail(request):
    id = request.session['userinfo']['user_id']
    u = UserInfo.objects.get(user_id=id)
    jieinfo = request.session.get('jieinfo')
    jieinfo.jie_user = u
    content = {
         'user_name': jieinfo.jie_user.user_name,
         'user_photo': jieinfo.jie_user.user_photo.url,
         'jie_section': jieinfo.jie_section,
         'jie_title': jieinfo.jie_titile,
         'jie_date': str(jieinfo.jie_date),
         'jie_kiss': jieinfo.jie_kiss,
         'jie_comment': jieinfo.jie_comment,
        }
    return render(request, 'jie/detail.html', content)
